package com.yang.mapper;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class NewMeta {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    public List<Map<String,Object>> searchAll() throws IOException {
        SearchRequest request = new SearchRequest("defect001");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());
        sourceBuilder.size(1000);
        request.source(sourceBuilder);

        ArrayList<Map<String,Object>> list = new ArrayList<>();

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        System.out.println(response.getHits().getTotalHits()+"总记录数");
        for (SearchHit hit : response.getHits().getHits()) {
            list.add(hit.getSourceAsMap());
        }
        System.out.println("ArrayList<Map<String,Object>>:大小" + list.size());
        return list;
    }
}

